<?php

namespace App\Http\Controllers\BusinessData;

use App\Http\Conf\ResultInfo;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Models\Business\ExternalOrderDetails;
use App\Http\Models\CategoryContract;
use App\Http\Requests\Business\ExternalOrderDetailsRequest;
use App\Imports\ExternalOrderDetailsImport;
use Exception;
use Maatwebsite\Excel\Facades\Excel;

class ExternalOrderDetailsController extends Controller
{
    private $select_where = [];
    private $request_params = [
        'year' => '',
        'payer_code' => '',
    ];

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $this->dealRequestParams($request);

        $query = ExternalOrderDetails::where($this->select_where);
        if (isset($request->delete) && count($this->select_where) > 0) {
            $query->delete();
        }

        $amount_total = $query->sum('amount');
        $models = $query->paginate(config('report.quantity_per_page'));

        return view('business_data.external_order_details.index', [
            'models' => $models,
            'amount_total' => $amount_total,
            'request_params' => $this->request_params,
        ]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $category_contract = CategoryContract::all();

        return view('business_data.external_order_details.create', [
            'category_contract' => $category_contract,
        ]);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(ExternalOrderDetailsRequest $request)
    {
        $model = new ExternalOrderDetails();
        $model->year = $request->year;
        $model->payer_code = $request->payer_code;
        $model->material_code = $request->material_code;
        $model->category_contract_id = $request->category_contract_id;
        $model->quantity = $request->quantity;
        $model->amount = $request->amount;
        if ($model->save()) {
            return redirect()->action('BusinessData\ExternalOrderDetailsController@show', $model);
        } else {
            dd("添加数据有异常！");
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $model = ExternalOrderDetails::find($id);
        return view('business_data.external_order_details.show', ['model' => $model]);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $category_contract = CategoryContract::all();
        $model = ExternalOrderDetails::find($id);
        return view('business_data.external_order_details.edit', [
            'model' => $model,
            'category_contract' => $category_contract,
        ]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(ExternalOrderDetailsRequest $request, $id)
    {
        $model = ExternalOrderDetails::find($id);
        $model->year = $request->year;
        $model->payer_code = $request->payer_code;
        $model->material_code = $request->material_code;
        $model->category_contract_id = $request->category_contract_id;
        $model->quantity = $request->quantity;
        $model->amount = $request->amount;
        if ($model->save()) {
            return redirect()->action('BusinessData\ExternalOrderDetailsController@show', $model);
        } else {
            dd("编辑数据有异常！");
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $model = ExternalOrderDetails::find($id);
        if ($model->delete()) {
            return ['status' => ResultInfo::SUCCESS];
        } else {
            return ['status' => ResultInfo::UNKNOWN];
        }
    }

    //导入开票明细数据
    public function import(Request $request)
    {
        if ($request->method() == 'GET') {
            return view('business_data.external_order_details.import');
        } elseif ($request->method() == 'POST') {
            if ($request->upload_file == null) {
                return redirect()->back()->with('fail', '请选择文件');
            }

            try {
                Excel::import(new ExternalOrderDetailsImport, request()->file('upload_file'));
            } catch (Exception $e) {
                return redirect()->back()->with('fail', $e->getMessage());
            }
            return redirect()->action('BusinessData\ExternalOrderDetailsController@index')->with('success', '导入数据成功！');
        }
    }


    /**
     * 处理请求参数
     */
    private function dealRequestParams(Request $request)
    {
        if (is_numeric($request->year)) {
            $this->select_where[] = ['year', $request->year];
            $this->request_params['year'] = $request->year;
        }

        if (is_numeric($request->payer_code)) {
            $this->select_where[] = ['payer_code', $request->payer_code];
            $this->request_params['payer_code'] = $request->payer_code;
        }
    }
}
